var ctx=window.daijia.ctx;

$(document).ready(function(){
	$("#start").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
	$("#end").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
	
	
	$('#companyIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择所属公司',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	$("input[name='showLocation']").change(function(){
		
		if($(this).val()=="activitycentre"){
			if($(this)[0].checked){
				$("#activitycentre_div").fadeIn(200);
			}else{
				$("#activitycentre_div").hide();
				
			}
		}else if($(this).val()=="business"){
			if($(this)[0].checked){
				$("#business_div").fadeIn(200);
			}else{
				$("#business_div").hide();
				
			}
		}
		
	});
	
	//图片上传
	$("#shareImage_btn").click(function(){
		$("#activityImage").click();				
	});
	
	$(document).on('change','#activityImage',function(){
		if($.trim($(this).val())){
			$("#shareImage_btn").html("上传中...");
			$("#shareImage_btn").attr("disabled",true);
		 	$.ajaxFileUpload({
                url:ctx+'/common/uploadimg.json',
                secureuri:false,
                fileElementId:'activityImage',
                dataType: 'json',
                data:{'upload_img_token':$("#upload_img_logo_token").val()},
                success: function (data) {

                	if(data.success){
                		$("#shareImage_btn").html("添加图片");
            			$("#shareImage_btn").attr("disabled",false);
                		$.scojs_message("上传成功", $.scojs_message.TYPE_OK);
                    	
                		$(".img").remove();
                		$("#centreActivityImage").val("/"+data.url);
                		$("#centre").append("<img id='myImage' class='img' src="+ctx+"/"+data.url+" width='165' height='96' />");
                	}
                	else{
                		$.scojs_message(result.msg, $.scojs_message.TYPE_ERROR);
                	}
                }
            });
		}
	});	

	
	$("#shareImage_btn2").click(function(){
		$("#activityImage2").click();				
	});
	
	$(document).on('change','#activityImage2',function(){
		if($.trim($(this).val())){
			$("#shareImage_btn2").html("上传中...");
			$("#shareImage_btn2").attr("disabled",true);
		 	$.ajaxFileUpload({
                url:ctx+'/common/uploadimg.json',
                secureuri:false,
                fileElementId:'activityImage2',
                dataType: 'json',
                success: function (data) {

                	if(data.success){
                		$("#shareImage_btn2").html("添加图片");
            			$("#shareImage_btn2").attr("disabled",false);
                		$.scojs_message("上传成功", $.scojs_message.TYPE_OK);
                    	
                		$(".img2").remove();
                		$("#businessActivityImage").val("/"+data.url);
                		$("#business").append("<img id='myImage2' class='img2' src="+ctx+"/"+data.url+" width='165' height='96' />");
                	}
                	else{
                		$.scojs_message(result.msg, $.scojs_message.TYPE_ERROR);
                	}
                }
            });
		}
	});	

	
	
	
	
	$("#submit_button").click(function(){

//		$("#centreActivityImage").val($(".img").attr("src"));
//		$("#businessActivityImage").val($(".img2").attr("src"));
		
		if(main){
			if($("#companyIds").val()==null){
				$.scojs_message("请选择所属公司", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}

		var theurl= /^((([hH][tT][tT][pP][sS]?|[fF][tT][pP])\:\/\/)?([\w\.\-]+(\:[\w\.\&%\$\-]+)*@)?((([^\s\(\)\<\>\\\"\.\[\]\,@;:]+)(\.[^\s\(\)\<\>\\\"\.\[\]\,@;:]+)*(\.[a-zA-Z]{2,4}))|((([01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([01]?\d{1,2}|2[0-4]\d|25[0-5])))(\b\:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)\b)?((\/[^\/][\w\.\,\?\'\\\/\+&%\$#\=~_\-@]*)*[^\.\,\?\"\'\(\)\[\]!;<>{}\s\x7F-\xFF])?)$/;
		if($("#chainedAddress").val()!=""){
			if(!theurl.test($("#chainedAddress").val())){
				$.scojs_message("链接地址格式不正确，如：www.baidu.com", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		
		if($("#activityTarget").val()==""){
			$.scojs_message("请选择业务类型", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if(""==$("#sort").val()){
			$.scojs_message("请填写排列顺序", $.scojs_message.TYPE_ERROR);
			return false;
		}
		if($("#sort").val()>6){
			$.scojs_message("排列顺序数字过大", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#imgsUrl").val()==""){
			$.scojs_message("请上传活动图片", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#start").val()==""){
		   $.scojs_message("请选择起始日期", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		
		if($("#end").val()!=""){
			if($("#start").val()>$("#end").val()){
				 $.scojs_message("起始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
		
		if($("#sort").val()){
			var reg= /^\+?[1-9][0-9]*$/;
			if($("#sort").val()=="" || !reg.test($("#sort").val())){
		       $.scojs_message('无效的数字,请填写大于0的整数！', $.scojs_message.TYPE_ERROR);
		       return false;
		    }
		}
		if(!$("input[name='showLocation']").prop('checked')){
			$.scojs_message("请选择展示位置,活动中心必选", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("input[name='showLocation']:checked").val()=="activitycentre"){
				if($("#centreActivityImage").val()==""){
					$.scojs_message("请上传活动中心图片", $.scojs_message.TYPE_ERROR);
					return false;
				}
		}
		if($("input[name='showLocation']:checked").val()=="business"){
			if($("#businessActivityImage").val()==""){
				$.scojs_message("请上传业务弹窗图片", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if(main){
			var companyNames=[];
			$("#companyIds option:selected").each(function(index,element){
				companyNames.push($(element).text());
			});
			$("#companyNames").val(companyNames.join(","));
		}
		
		$.post(ctx+'/activitymanage/update',$("#submit_form").serialize(),function(data){
			if(data.success){
				$.scojs_message("修改成功", $.scojs_message.TYPE_OK);
				window.location=ctx+"/activitymanage/activitymanage";
			}
			else{
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				$("#submit_button").attr("disabled",false);
			}
		});
		
	});
	
	

});